
var myNick = null;

$(document).ready(function() {
    getUserInfo();
    
    $("#storepic").change(function(){
        var files = this.files;
        if(files.length == 1) {
            //document.getElementById("newPicData").submit();
            postFile("headPic.user", files[0], function(data) {
                switch(data.type) {
                    case "headPicChanged":
                        $("#headPic").css("background-image", "url(\"" + URL.createObjectURL(files[0]) + "\")");
                        break;
                    default:
                    case "headPicNotChanged":
                        break;
                }
            });
        }
    });
    //changeNickName
    
    $("#unick").blur(function() {
        if(myNick != null && myNick != this.value && nickTest(this.value)) {
            changeNickName(this.value);
        } else {
            $(this).addClass("invalid");
        }
    });

    $("#unick").focus(function() {
        $(this).removeClass("invalid");
    });
    
    $("#upass").blur(function() {
        
        if(accountStrTest(this.value)) {
            changePassword(sha256.hex(this.value));
            $(this).addClass("hid");
            $("#btnChangePassword").removeClass("hid");
        } else {
            $(this).addClass("invalid");
        }
    });

    $("#upass").focus(function() {
        $(this).removeClass("invalid");
    });

    $("#btnChangePassword").click(function() {
        var paswod = $("#upass");
        paswod.removeClass("hid");
        paswod.val("");
        paswod.focus();
        $(this).addClass("hid");
    });

    $("#btnJoinRank").click(function() {
        joinRank();
    });

    $("#btnLogout").click(function() {
        logout();
    });
});

function joinRank() {
    $.ajax({
        url: "joinRank.user",
        type: "post",
		dataType: "json",
        success: function(data) {
            switch(data.type) {
                case "userGetRank":
                    updateRank(data);
                    break;
                default:
                case "userLost":
                    location.href = "/Z5COnline/battleground.html";
            }
        }
    });
}

function getUserRank() {
    $.ajax({
        url: "getRank.user",
        type: "post",
		dataType: "json",
        success: function(data) {
            switch(data.type) {
                case "userGetRank":
                    updateRank(data);
                    break;
                default:
                case "userLost":
                    location.href = "/Z5COnline/battleground.html";
            }
        }
    });
}

function updateRank(jsonData) {
    if(jsonData.rank != null) {
        let urank = $("#urank");
        urank.text("Rank: " + jsonData.rank + "/" + jsonData.rankSize);
        $("#btnJoinRank").addClass("hid");
        urank.removeClass("hid");
        urank[0].onclick = goRank;
    }
}

function getUserInfo() {
    $.ajax({
        url: "get.user",
        type: "post",
		dataType: "json",
        success: function(data) {
            switch(data.type) {
                case "userGet":
                    $("#unick").val(data.usernick);
                    $("#uname").text(data.username);
                    $("#uid").text("uid:" + data.userid);
                    $("#uscore").text(data.score);
                    $("#ulost").text("Lost: " + data.lost);
                    $("#uwin").text("Win: " + data.win);
                    $("#urun").text("Run: " + data.run);
                    myNick = data.username;

                    updateRank(data);

                    if(data.userheadPic != null  &&  data.userheadPic != "") {
                        /*var headPicUrl = location.protocol + "//" +  
                            location.hostname + ":" +
                			location.port + "/" +
                			projectRootDir(location.pathname) + 
                            "/data/z5col/z5col_files/pictures/" +
                            data.userheadPic;
                        $("#headPic").css("background-image", "url(\"" + headPicUrl + "\")");
                        */
                        $("#headPic").css("background-image", headPicPathname(data.userheadPic));
                    }
                    break;
                default:
                case "userLost":
                    location.href = "/Z5COnline/battleground.html";
            }
        }
    });
}

function changeNickName(unick) {
    $.ajax({
        url: "nick.user",
        type: "post",
        data: {
            newNick:unick
        },
		dataType:"json",
        success: function(data) {
            switch(data.type) {
                case "nickchanged":
                    myNick = $("#unick").val();
                    break;
                case "nickNotchanged":
                    $("#unick").addClass("invalid");
                    break;
            }
        }
    });
}

function changePassword(newName) {
    $.ajax({
        url: "password.user",
        type: "post",
        data: {
            newpass:newName
        },
		dataType:"json",
        success: function(data) {
            switch(data.type) {
                case "passchanged":
                    break;
                case "passNotchanged":
                    $("#upass").addClass("invalid");
                    break;
            }
        }
    });
}

function logout() {
    $.ajax({
        url:"logout.user",
        type:"post",
		dataType:"json",
        success: function(data) {
			responseProcess(data);
        }
    });
}

function postFile(addr, file, successCallback) {
    var formData = new FormData();
    formData.append("file", file);
    $.ajax({
        url: addr,
        type: "post",
        data: formData,
        processData: false, // 告诉jQuery不要去处理发送的数据
        contentType: false, // 告诉jQuery不要去设置Content-Type请求头
        dataType: "json",
        success: successCallback
    });
}